package com.dianrui.firemenmonitor.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.firemenmonitor.R;
import com.dianrui.firemenmonitor.bean.MainArticleTop;
import com.dianrui.firemenmonitor.util.GlideUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainArticleAdapter extends BaseAdapter {
    private List<MainArticleTop> mainArticlesList;
    private Context context;
    private MainArticleClickListener mainArticleClickListener;

    public MainArticleAdapter(Context context, List<MainArticleTop> mainArticlesList) {
        this.context = context;
        this.mainArticlesList = mainArticlesList;
    }

    public void refresh(List<MainArticleTop> list) {
        this.mainArticlesList = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mainArticlesList.size();
    }

    @Override
    public Object getItem(int position) {
        return mainArticlesList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public interface MainArticleClickListener {
        void onMainArticleClick(String htmlurl,String titles);

    }

    public void setOnButtonClickListener(MainArticleClickListener mainArticleClickListener) {
        this.mainArticleClickListener = mainArticleClickListener;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_home_top_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (null != mainArticlesList && mainArticlesList.size() > 0) {
            final MainArticleTop mainArticleTop = mainArticlesList.get(position);
            holder.postTitle.setText(mainArticleTop.post_title);
            GlideUtil.loadImageView(parent.getContext().getApplicationContext(),mainArticleTop.thumbnail,holder.thumbnail);
            String dates = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date(mainArticleTop.create_time * 1000));
            holder.time.setText(dates);
            holder.mainarticleLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mainArticleClickListener != null) {
                        mainArticleClickListener.onMainArticleClick(mainArticleTop.html_href,mainArticleTop.post_title);
                    }
                }
            });


        }
        return convertView;
    }


     class ViewHolder {
        @BindView(R.id.thumbnail)
        ImageView thumbnail;
        @BindView(R.id.post_title)
        TextView postTitle;
        @BindView(R.id.time)
        TextView time;
         @BindView(R.id.mainarticle_layout)
        LinearLayout mainarticleLayout;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
